package com.cup.wx.micro.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.cup.wx.config.WebChatConfig.WXAccountProperties;
import com.cup.wx.config.WebChatConfig.WXUrlProperties;
import com.cup.wx.micro.bean.WxAccessToken;
import com.cup.wx.micro.service.WXBaseService;
import com.cup.wx.micro.service.WXMessageService;
/**
 * 
 * @Title:  WebchatMessageServiceImpl.java   
 * @author: alan.deyu.zhao
 * @date:   Jan 3, 2019
 */
@EnableConfigurationProperties({WXAccountProperties.class})
public class WXMessageServiceImpl implements WXMessageService {
	
	@Autowired
	private WXUrlProperties wxurl;
	
	@Autowired
	private WXBaseService wxBaseService;
	
	@Autowired
	private RestTemplate restTemplate;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Map<String, Object> setIndustry(String industry_id1, String industry_id2) {
		
		Map<String, Object> postData = new HashMap<>();
		postData.put("industry_id1", industry_id1);
		postData.put("industry_id2", industry_id2);
		
		WxAccessToken acesstoken = wxBaseService.getRedisAcessToken();
		
		String requestUrl = wxurl.getDomain() + String.format(wxurl.getSetindustry(), acesstoken.getAccessToken());
		
		ResponseEntity<Map> rs = restTemplate.postForEntity(requestUrl, JSON.toJSONString(postData), Map.class);
		
		Map<String, Object> itemMap = rs.getBody();
		
		return itemMap;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Map<String, Object> getIndustry() {
		
		WxAccessToken acesstoken = wxBaseService.getRedisAcessToken();
		
		String requestUrl = wxurl.getDomain() + String.format(wxurl.getGetindustry(), acesstoken.getAccessToken());
		
		ResponseEntity<Map> rs = restTemplate.getForEntity(requestUrl,  Map.class);
		
		return rs.getBody();
	}

	@Override
	public String getIndustryTemplateId(String templateIdShort) {
		
//		Map<String, Object> postData = new HashMap<>();
//		postData.put("template_id_short", templateIdShort);
//		
//		String requestUrl = WebchatConfig.API_ADD_TEMPLATE.replace("ACCESS_TOKEN", 
//    			WebchatConfig.accessToken.getAccessToken()); 
//		
//		JSONObject json = HttpUtil.doPoststr(WebchatConfig.DOMAIN_API + requestUrl , JSON.toJSONString(postData));
		
//		return json.getString("template_id");
		
		return null;
	}

	@Override
	public List<Map<String, Object>> getIndustryTemplateList() {
		
//		String requestUrl = WebchatConfig.API_GET_INDUSTRY.replace("ACCESS_TOKEN", 
//    			WebchatConfig.accessToken.getAccessToken()); 
//		
//		JSONObject json = HttpUtil.doGetstr(WebchatConfig.DOMAIN_API + requestUrl);
//		
//		Map<String, Object> itemMap = JSON.parseObject(json.toJSONString());
//		
//		@SuppressWarnings("unchecked")
//		List<Map<String, Object>> templist = (List<Map<String, Object>>) itemMap.get("template_list");
//		return templist;
		return null;
	}

	@Override
	public boolean sendTemplateMessage(String openid, String templateId, String url, String appid, String pagepath,
			Map<String, Object> datas) {
		
		// 1. validate params
//		if(StringUtils.isEmpty(openid) || StringUtils.isEmpty(templateId) || datas.isEmpty()){
//			return false;
//		}
//		
//		Map<String, Object> postData = new HashMap<>();
//		postData.put("touser", openid); // 接收者openid
//		postData.put("template_id", templateId);
//		postData.put("url", url);
//		if(!StringUtils.isEmpty(appid)){
//			// 优先跳转至小程序
//			// 当用户的微信客户端版本不支持跳小程序时，将会跳转至url
//			Map<String, Object> miniprogram = new HashMap<>();
//			miniprogram.put("appid", appid);
//			miniprogram.put("pagepath", pagepath);
//			postData.put("miniprogram", miniprogram);
//		}
//		postData.put("data", datas);n
//		
//		String requestUrl = WebchatConfig.API_SEND_MESSAGE.replace("ACCESS_TOKEN", 
//    			WebchatConfig.accessToken.getAccessToken()); 
//		
//		JSONObject json = HttpUtil.doPoststr(WebchatConfig.DOMAIN_API + requestUrl , JSON.toJSONString(postData));
//		
//		if("0".equals(json.getString("errcode"))){
//			return true;
//		}else{
//			logger.error("Send Template Message Failed, Reason : " + json.getString("errmsg"));
//		}
		
		return false;
	}

}
